Option Explicit

'-----------------------
' 全局变量
'-----------------------
Private globalFSO As Object

' ==================================================
' Map
' ==================================================
' 创建一个 vba 内置类型的 `Dictionary` 对象作为 map 对象
Public Function createMap() As Object
    Set createMap = CreateObject("Scripting.Dictionary")
End Function

' ==================================================
' Wscript.shell
' ==================================================
' 创建一个 shell 对象
Public Function createShell() As Object
    Set createShell = CreateObject("Wscript.shell")
End Function

' ==================================================
' Regexp
' ==================================================
Public Function createRegexp() As Object
    Set createRegexp = CreateObject("vbscript.regexp")
End Function

' ==================================================
' ADODB.Stream
' ==================================================
' 创建 ADODB.Stream 对象
Public Function createAdodbStream() As Object
    Set createAdodbStream = CreateObject("ADODB.Stream")
End Function

' 关闭一个 ADODB.Stream 对象
Public Sub closeAdodbStream(ByRef stream As Object)
    If stream Is Nothing Then
        Exit Sub
    End If

    If stream.State <> 0 Then
        stream.Close
    End If
End Sub

' ==================================================
' FileSystemObject
' ==================================================
' 创建 FileSystemObject 对象，全局单例！
Public Function getGlobalFSO() As Object
    If globalFSO Is Nothing Then
        Set globalFSO = CreateObject("Scripting.FileSystemObject")
    End If

    Set getGlobalFSO = globalFSO
End Function

' 删除全局单例的 FileSystemObject 对象
Public Sub delGlobalFSO()
    If Not globalFSO Is Nothing Then
        Set globalFSO = Nothing
    End If
End Sub

' 创建 FileSystemObject 对象
Public Function createFSO() As Object
    Set createFSO = CreateObject("Scripting.FileSystemObject")
End Function

' ==================================================
' Excel.Application
' ==================================================
' 创建一个新的 excel 进程
' @optional-param visible 设置这个进程是否可见(是否显示在任务栏中)
Public Function createExcelApplication(Optional ByRef visible As Boolean = False) As Excel.Application
    Set createExcelApplication = CreateObject("Excel.Application")
    createExcelApplication.Visible = visible
End Function

' 关闭一个 excel 进程
' @param app excel 进程对象
Public Sub closeExcelApplication(ByRef app As Excel.Application)
    If app Is Nothing Then
        Exit Sub
    End If

    With app
        .UserControl = True
        .Quit
    End With
End Sub

